package com.etb.wisdom.eframe.dict.service.impl;

import org.springframework.stereotype.Service;

import com.etb.core.service.ETBService;
import com.etb.core.utils.SequenceUtil;
import com.etb.wisdom.eframe.dict.service.EtbDictinfoService;
import com.etb.wisdom.eframe.model.EtbDictinfo;
import com.etb.wisdom.eframe.model.EtbDicttype;

@Service
public class EtbDictinfoServiceImpl extends ETBService implements EtbDictinfoService {

	@Override
	public void save(EtbDictinfo info) {
		service.save(info);
		if(info.getUplevelid() == null) {
			info.setInfoseq(info.getId());
		} else {
			EtbDictinfo uplevelInfo = this.getUplevelInfo(info);
			info.setInfoseq(SequenceUtil.generateNewSequence(uplevelInfo.getInfoseq(), info.getId()));
		}
		service.save(info);
	}
	
	@Override
	public EtbDictinfo getUplevelInfo(EtbDictinfo info) {
		EtbDicttype type = new EtbDicttype();
		type.setId(info.getDicttypeid());
		service.extend(type);
		EtbDictinfo uplevelInfo = new EtbDictinfo();
		uplevelInfo.setDicttypeid(type.getUplevelid());
		uplevelInfo.setInfocode(info.getUplevelid());
		service.extend(uplevelInfo);
		return uplevelInfo;
	}
}
